const BehaviorAll = require("../../../behaviors/behavior_all.js");
const ServiceProduct = require("../../../service/service_product.js");
const ServiceScore = require("../../../service/service_score.js");
const ServiceOrder = require("../../../service/service_order.js");
const ServiceUser = require("../../../service/service_user.js");

Component({
  /**
   * behaviors
   */
  behaviors: [BehaviorAll],
  /**
   * 页面的初始数据
   */
  data: {
    allScore: 0,
    /**
     * 产品滚动图
     * 数据格式：
        [
          {id:1, url: "https://img1.360buyimg.com/pop/jfs/t25117/290/2321508907/55387/35d9d12/5bc844a6Nc2d978ab.jpg"},
          {id: 2, url: "https://img20.360buyimg.com/da/jfs/t18685/238/1455346844/110119/f5bc9082/5acb3b0eN79be7f6e.jpg"},
          {id: 3, url: "https://m.360buyimg.com/babel/jfs/t25696/263/2271754861/46876/599f49bf/5bc73b97N53c4b287.jpg"}
        ]
     */
    slideImgs: [],
    /**
     * 产品
     * 数据格式：
        {
          id: 1,
          name: "法国进口红酒拉斐天使庄园干红葡萄酒红酒整箱礼盒装750ml*6",
          price: 100,
          detail: '<ol class=" list-paddingleft-2" style="list-style-type: decimal;"><li><p><span style="text-decoration: underline; color: rgb(255, 0, 0);"><em><strong>abc<img src="http://img.baidu.com/hi/jx2/j_0015.gif" /></strong></em></span></p></li ><li><p><span style="color: rgb(255, 0, 0); border: 1px solid rgb(0, 0, 0); text-decoration: line-through; background-color: rgb(255, 255, 0);"><em><strong>asdfbb</strong></em></span></p></li></ol >'
        }
     */
    product: null,
    /**
     * 购物车相关
     */
    triggerAddData: null,
    triggerRemoveData: null,
    triggerRetrieveData: null,
    car_amount: 0,
    allDataMap: {}
  },

  methods: {
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      /** 获取数据 */
      const _this = this;
      const id = options.id;
      // 加载数据
      ServiceScore.find(function (data) {
        _this.setData({
          allScore: data
        })
      })
      ServiceUser.getInfo(function (data) {
        _this.setData({
          userInfo: data
        })
      });
      //
      ServiceProduct.listSlideImg(id, function (data) {
        _this.setData({ slideImgs: data.records })
      });
      //
      ServiceProduct.findById(id, function (data) {
        _this.setData({ product: data });
      });
    },
    /**
     * onShow
     */
    onShow: function (e) {
      const _this = this;
      _this.setData({ triggerRetrieveData: true });
    },

    showImg: function (event) {
      const src = event.currentTarget.dataset.src;
      wx.previewImage({
        current: src, // 当前显示图片的http链接
        urls: [src] // 需要预览的图片http链接列表
      })
    },
    /**
     * 添加到购物车
     */
    onAdd: function (e) {
      const _this = this;
      const productId = _this.data.product.id;
      ServiceProduct.findById(productId, function (data) {
        _this.setData({
          triggerAddData: {
            id: data.id,
            name: data.name,
            value: data.score,
            amount: data.amount,
            imgUrl: data.imgUrl,
            amount: 1,
            selected: true,
            describe: '每件需' + data.score + '易物值'
          }
        })
      });
    },
    /**
     * 从购物车移除
     */
    onRemove: function (e) {
      const _this = this;
      const productId = _this.data.product.id;
      _this.setData({
        triggerRemoveData: { id: productId }
      })
    },
    /**
     * 数据改变
     */
    car_onChange: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 获取数据
     */
    car_onRetrieve: function (e) {
      const _this = this;
      _this.setData({
        car_amount: e.detail.amount,
        allDataMap: e.detail.allDataMap
      })
    },
    /**
     * 提交兑换
     */
    onSubmitOrder: function (e) {
      const _this = this;
      const products = [{ id: _this.data.product.id, amount: 1 }];
      ServiceOrder.createGrade(products, function (data) {
        wx.navigateTo({
          url: '../pay/pay?id=' + data.id,
        })
      });
    }
  }
  
})